// pages/management/billFill/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    billList: [],
    recordList: []
  },

  getData() {
    wx.$apis.getAccountBookInfo(this.data.id).then((res) => {
      const { data } = res;
      let billList = (item) => {
        // 收入 + 未结 - 支出 = 合计利润，不管是否为负数
        let incomeTotal = Math.round(((item.incomeAmount * 1 + item.unpaidAmount * 1 - item.paidAmount) / 100) * 100) / 100;
        return [
          {
            ...item,
            incomeAmount: Math.round((item.incomeAmount / 100) * 100) / 100,
            unpaidAmount: Math.round((item.unpaidAmount / 100) * 100) / 100,
            paidAmount: Math.round((item.paidAmount / 100) * 100) / 100,
            incomeTotal,
            createTime: wx.$dayjs(item.createTime).format("YYYY-MM-DD HH:mm:ss")
          }
        ];
      };
      this.setData({
        billList: billList(data)
      });
    });
  },

  // 分页查询 收入/支出 记录
  getAccountBookRecord() {
    let params = {
      accountBookId: this.data.id,
      asc: true,
      columns: [],
      current: 1,
      currentMongo: 0,
      size: 200
    };
    wx.showLoading({
      title: "查询中..."
    });
    wx.$apis.getAccountBookRecord(params).then((res) => {
      if (res.data) {
        const { records } = res.data;
        this.setData({
          recordList: records.map((item) => {
            return {
              ...item,
              recordTime: wx.$dayjs(item.recordTime).format("YYYY-MM-DD HH:mm:ss"),
              amount: wx.$getYuan(item.amount),
              unitPrice: wx.$getYuan(item.unitPrice)
            };
          })
        });
      }
    });
  },

  /**
   * 账单收入
   */
  toBillIncome(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/management/bill-income/index?id=" + id
    });
  },

  /**
   * 账单支出
   */
  toBillPay(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/management/bill-pay/index?id=" + id
    });
  },

  // 收入支出记录
  toRecordDetail(e) {
    const { id, type } = e.currentTarget.dataset;
    let query = "?id=" + this.data.id + "&recordId=" + id;
    let url = "/pages/management/bill-income/index" + query;
    // 支出
    if (type == 1) {
      url = "/pages/management/bill-pay/index" + query;
    } else {
      // 收入
      url = "/pages/management/bill-income/index" + query;
    }
    wx.navigateTo({
      url
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    this.setData({
      id
    });
  },

  onShow() {
    this.getData();
    this.getAccountBookRecord();
  },

  // 关闭账本
  onCloseBill() {
    wx.showLoading({
      title: "关闭中..."
    });
    wx.$apis
      .editAccountBook({
        id: this.data.id,
        status: 3
      })
      .then((res) => {
        wx.showToast({
          title: "关闭成功"
        });
        wx.navigateBack();
      });
  },

  // 删除账本
  onDeleteBill() {
    wx.showLoading({
      title: "删除中..."
    });
    wx.$apis.deleteAccountBook(this.data.id).then((res) => {
      wx.showToast({
        title: "删除成功"
      });
      wx.navigateBack();
    });
  }
});
